package com.xxx.car.service.impl;

import com.xxx.car.service.CommonService;
import com.xxx.car.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * Created by yxj on 17/6/18.
 */
@Service
public class CommonServiceImpl implements CommonService {
	@Value("${upload.filePath}")
	private String filePath;

	@Override
	public String uploadFile(MultipartFile file) throws Exception{
		//文件前缀
		String fileNamePrefix = UUIDUtils.createId();
		//文件后缀
		String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
		String newFileName = fileNamePrefix+"."+suffix;
		Files.copy(file.getInputStream(), Paths.get(filePath, newFileName));
		if (file.getOriginalFilename() != null && file.getOriginalFilename() != ""){
			return newFileName;
		} else {
			return null;
		}
	}

	public static void main(String[] args) {
		String fileName = "abc.txt";
		String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
		System.out.println(suffix);
	}
}
